/*
 * @Description: 系统消息组件 SystemMessage
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-26 18:26:05
 * @LastEditors: zhengzhuang
 * @LastEditTime: 2019-11-23 12:50:09
 */

import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/config/service_api.dart';
import 'package:provider/provider.dart';
import 'package:C20Driver/model/get_chat_list.dart';
import 'package:C20Driver/provider/message_center_provider.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/service_method.dart';
import 'package:C20Driver/widget/spinkit.dart';

class SystemMessage extends StatefulWidget {
  @override
  _SystemMessageState createState() => _SystemMessageState();
}

class _SystemMessageState extends State<SystemMessage>
    with AutomaticKeepAliveClientMixin {
  final AsyncMemoizer _memoizer = AsyncMemoizer();

  Future<void> _getChatList() async {
    return _memoizer.runOnce(() async {
      return await DioUtils()
          .request(ReqMethod.POST, ServiceApi.getChatList, params: {
        'page': '1',
        'pageSize': '10',
      }, success: (res) async {
        Provider.of<MessageCenterProvider>(context).setMessage(res);
        return res;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    MessageCenterProvider provider =
        Provider.of<MessageCenterProvider>(context);
    return FutureBuilder(
      future: _getChatList(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          return Container(
            child: EasyRefresh(
              header: ClassicalHeader(
                bgColor: Colors.white,
                textColor: Colors.black,
                noMoreText: '',
                refreshText: '下拉刷新',
                refreshReadyText: '松开刷新',
                refreshingText: '正在刷新中',
                refreshedText: '刷新完成',
                showInfo: false,
              ),
              footer: ClassicalFooter(
                bgColor: Colors.white,
                textColor: Colors.black,
                noMoreText: '',
                loadText: '下拉刷新',
                loadReadyText: '松开刷新',
                loadingText: '正在加载中',
                loadedText: '加载完成',
                showInfo: false,
              ),
              child: ListView.builder(
                itemCount: provider.notification.length,
                itemBuilder: (BuildContext context, index) {
                  return _messageList(context,
                      GetChatList.fromJson(provider.notification[index]));
                },
              ),
              onRefresh: () async => provider.dropDownRefreshMessage(),
              onLoad: () async => provider.pullUpLoadingMessage(),
            ),
          );
        } else {
          return Spinkit();
        }
      },
    );
  }

  // 消息列表
  Widget _messageList(context, index) {
    return Container(
      margin: EdgeInsets.only(
        top: ScreenUtil().setWidth(10.0),
        left: ScreenUtil().setWidth(25.0),
        right: ScreenUtil().setWidth(25.0),
        bottom: ScreenUtil().setWidth(10.0),
      ),
      child: InkWell(
        child: Container(
          width: ScreenUtil().setWidth(700.0),
          height: ScreenUtil().setWidth(160.0),
          padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(20.0),
            right: ScreenUtil().setWidth(20.0),
          ),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10.0)),
            boxShadow: <BoxShadow>[
              BoxShadow(
                color: Colors.black12,
                blurRadius: ScreenUtil().setWidth(20.0),
              ),
            ],
          ),
          child: Column(
            children: <Widget>[
              SizedBox(
                height: ScreenUtil().setWidth(30.0),
              ),
              _intermediateRegion(index),
            ],
          ),
        ),
        onTap: () {
          NavigatorUtil.jump(context,
              '/detailsMessage?orderId=${index.orderId}&userName=${Uri.encodeComponent(index.userName)}');
        },
      ),
    );
  }

  // 内容
  Widget _intermediateRegion(index) {
    return Row(
      children: <Widget>[
        ClipOval(
          child: Image.network(
            index.headimgurl != null ? index.headimgurl : '',
            width: ScreenUtil().setWidth(100.0),
            height: ScreenUtil().setWidth(100.0),
          ),
        ),
        SizedBox(
          width: ScreenUtil().setWidth(10.0),
        ),
        Expanded(
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      index.userName != null ? index.userName : '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(30.0),
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                  ),
                  Container(
                    child: Text(
                      index.updateTime != null ? index.updateTime : '',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(26.0),
                        color: Colors.black54,
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: ScreenUtil().setWidth(20.0),
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      index.lastMessage != null ? index.lastMessage : '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(30.0),
                        color: Color.fromARGB(255, 153, 153, 153),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: index.unread == 0,
                    child: CircleAvatar(
                      radius: ScreenUtil().setWidth(20.0),
                      backgroundColor: Colors.red,
                      child: Text(
                        index.unread.toString(),
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: ScreenUtil().setSp(24.0),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  @override
  bool get wantKeepAlive => true;
}
